package com.atguigu.stgy.admin.service.impl;

import com.atguigu.stgy.admin.service.*;
import com.atguigu.stgy.model.dto.ApartmentDTO;
import com.atguigu.stgy.model.dto.ApartmentSearchDTO;
import com.atguigu.stgy.model.entity.*;
import com.atguigu.stgy.model.vo.ApartmentVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.stgy.admin.mapper.ApartmentInfoMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {
    @Resource
    ProvinceInfoService provinceInfoService;

    @Resource
    CityInfoService cityInfoService;

    @Resource
    DistrictInfoService districtInfoService;
    @Resource
    ApartmentFacilityService apartmentFacilityService;

    @Resource
    ApartmentLabelService apartmentLabelService;

    @Resource

    ApartmentFeeValueService apartmentFeeValueService;

    @Resource
    GraphInfoService graphInfoService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdateApartmentDTO(ApartmentDTO apartmentDTO) {
        ApartmentInfo apartmentInfo = new ApartmentInfo();
        BeanUtils.copyProperties(apartmentDTO, apartmentInfo);


        apartmentInfo.setIsRelease(apartmentDTO.getIsRelease());


        // 获取省市区的名称
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentInfo.getProvinceId());
        CityInfo cityInfo = cityInfoService.getById(apartmentInfo.getCityId());
        DistrictInfo districtInfo = districtInfoService.getById(apartmentInfo.getDistrictId());

        // 设置省市区的名称为
        apartmentInfo.setProvinceName(provinceInfo.getName());
        apartmentInfo.setCityName(cityInfo.getName());
        apartmentInfo.setDistrictName(districtInfo.getName());


        this.save(apartmentInfo);
        Long apartmentInfoId = apartmentInfo.getId();

        apartmentFacilityService.bathSaveFacility(apartmentDTO, apartmentInfoId);


        apartmentLabelService.bathSaveLabel(apartmentDTO, apartmentInfoId);

        //  apartment_fee_value
        apartmentFeeValueService.bathSaveFeeValue(apartmentDTO, apartmentInfoId);

        graphInfoService.batchSaveGraphInfo(apartmentDTO,apartmentInfoId);



    }

    @Override

    public Page<ApartmentVO> pageItem(ApartmentSearchDTO searchDTO) {

        Page<ApartmentVO> page = new Page<>(searchDTO.getCurrent(), searchDTO.getSize());
        List<ApartmentVO> list = baseMapper.selectApartmentVOByCondition(page, searchDTO);
            page.setRecords(list);
        return page;
    }


}
